<?php

$InstructionTitle = "Prescriptions";
$InstructionDetails ="Are any of your prescription medications missing from this list?";

$BreadCrumb = TRUE;
$welcome = "BC-next";
$prescriptions = "BC-current";
$nonprescriptions = "BC-next";

$ButtonText = "Submit";
$ContinuePage = "missing-prescriptions-add.php?NonPrescription=0";
$NextPage = "nonprescriptions.php";
$LargeButton1Text = "Yes &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;";
$LargeButton2Text = "Continue...&#160;&#160;";

?>

<?php 
   include 'includes/wizard-simple-header.php';
?>



<?php
  $fields = "MedicationType, MedicationSubType, MedicationIdentity, GenericIngredients, UnitStrength, DosageForm, Route, DispensedQuantity, RefillsAuthorized, DosingFrequency, DosingQuantity, DosingInstructions, FullTextDescription, OutOfPocketCostUS, AnnotationReferences, DosingStatus, EffectiveStartDate, EffectiveEndDate, Provenance, inList";

if (isset($_GET['AddMed'])){

  include 'includes/db-login.php';

  $AddMed = $_GET['AddMed']; 
  //  echo $AddMed;
  
  // add the medication sent to us
  list($MedicationIdentity, $GenericIngredients, $UnitStrength) = explode("~", $AddMed);

  $QueryPieces = explode("~", $AddMed);
  $InsertQueryPieces = explode("~", $AddMed);
  $QueryPiecesTotal = count($QueryPieces);
  $QueryPiecesCount = 0;
  
  // check for empty fields and replace them with NULL for database entry
  while ($QueryPiecesCount < $QueryPiecesTotal) {
    if (strlen($QueryPieces[$QueryPiecesCount]) == 0){
      $QueryPieces[$QueryPiecesCount] = " is NULL";
      $InsertQueryPieces[$QueryPiecesCount] = "NULL";
    } else {
      $QueryPieces[$QueryPiecesCount] = " = '".$QueryPieces[$QueryPiecesCount]."'";
      $InsertQueryPieces[$QueryPiecesCount] = "'".$InsertQueryPieces[$QueryPiecesCount]."'";
    }
    $QueryPiecesCount++;
  } // while $QueryPiecesCount

      
  //Put it in the medication list
  $query = "SELECT * FROM medications WHERE MedicationType = 'prescription' AND MedicationIdentity ".$QueryPieces[0]." AND GenericIngredients ".$QueryPieces[1]." AND UnitStrength ".$QueryPieces[2];
  //  echo $query;


  $result = mysql_query($query) or die('Query failed: ' . mysql_error());

  $numresults = mysql_num_rows($result);

  // If medication not in list (no med coming up with that name), put it in the list
  // If the medication is in the database, change inList to TRUE once
  if ($numresults == 0){
    //inserting into DB
    $inList = "TRUE";
    //INSERT INTO medications (MedicationType, MedicationIdentity,
    //GenericIngredients, UnitStrength, inList) VALUES
    //('prescription', 'Premarin', NULL, NULL, TRUE);
    // QUESTION: Should this provenance be PersonalSource?
    $query = "INSERT INTO medications (MedicationType, MedicationIdentity,
GenericIngredients, UnitStrength, inList) VALUES ('prescription',".$InsertQueryPieces[0].", ".$InsertQueryPieces[1].", ".$InsertQueryPieces[2].", ".$inList.")";	
    
    //    echo "<BR>numResults=0: ".$query."<BR>";

    $InsertResult = mysql_query($query) or die('Query failed: ' . mysql_error());
  } else {
    $row = mysql_fetch_array($result);
    //Should only be one in the database, but there may be more
    // We're only going to set one InList to true so there aren't multiple meds on the list
    $query = "UPDATE medications SET inList = TRUE WHERE ID = ".$row['ID'];
    $UpdateResult = mysql_query($query) or die('Query failed: ' . mysql_error());
  }

  // have to create a med list from what is already there...

  include 'includes/create-med-list.php';


  include 'includes/db-close.php';


 } // end if isset AddMed
 else{
if(isset($_POST['RxMeds']))
{
?>
<div id="medication-list">
<?php
  include 'includes/db-login.php';


   $RxMeds = $_POST['RxMeds'];
   $n        = count($RxMeds);
   $i        = 0;

   echo "<u>My Medication List</u>\r\n" .
        "<ol>";
   while ($i < $n)
   {
     //      echo "<li>{$RxMeds[$i]}</li> \r\n";
      
      //We sent everything with a delimiter of "."
      //Use Explode to get the pieces back
      list($MedicationType, $MedicationSubType, $MedicationIdentity, $GenericIngredients, $UnitStrength, $DosageForm, $Route, $DispensedQuantitity, $RefillsAuthorized, $DosingFrequency, $DosingQuantity, $DosingInstructions, $FullTextDescription, $OutOfPocketCostUS, $AnnotationReferences, $DosingStatus, $EffectiveStartDate, $EffectiveEndDate, $Provenance, $inList) = explode(".", $RxMeds[$i]);

      $QueryPieces = explode(".", $RxMeds[$i]);
      $InsertQueryPieces = explode(".", $RxMeds[$i]);
      $QueryPiecesTotal = count($QueryPieces);
      $QueryPiecesCount = 0;

      // check for empty fields and replace them with NULL for database entry
      while ($QueryPiecesCount < $QueryPiecesTotal) {
	if (strlen($QueryPieces[$QueryPiecesCount]) == 0){
	  $QueryPieces[$QueryPiecesCount] = " is NULL";
	  $InsertQueryPieces[$QueryPiecesCount] = "NULL";
	} else {
  	  $QueryPieces[$QueryPiecesCount] = " = '".$QueryPieces[$QueryPiecesCount]."'";
  	  $InsertQueryPieces[$QueryPiecesCount] = "'".$InsertQueryPieces[$QueryPiecesCount]."'";
	}
	$QueryPiecesCount++;
      } // while $QueryPiecesCount

      echo "<LI>{$MedicationIdentity} ({$UnitStrength})<BR><i>({$GenericIngredients})</i></LI>\r\n";
      
      //Put it in the medication list
      $query = "SELECT * FROM medications WHERE MedicationType".$QueryPieces[0]." AND MedicationSubType".$QueryPieces[1]." AND MedicationIdentity ".$QueryPieces[2]." AND GenericIngredients ".$QueryPieces[3]." AND UnitStrength ".$QueryPieces[4]." AND DosageForm ".$QueryPieces[5]." AND Route ".$QueryPieces[6]." AND DispensedQuantity ".$QueryPieces[7]." AND RefillsAuthorized ".$QueryPieces[8]." AND DosingFrequency ".$QueryPieces[9]." AND DosingQuantity ".$QueryPieces[10]." AND DosingInstructions ".$QueryPieces[11]." AND FullTextDescription ".$QueryPieces[12]." AND OutOfPocketCostUS ".$QueryPieces[13]." AND AnnotationReferences ".$QueryPieces[14]." AND DosingStatus ".$QueryPieces[15]." AND EffectiveStartDate ".$QueryPieces[16]." AND EffectiveEndDate ".$QueryPieces[17]." AND Provenance ".$QueryPieces[18];

      $result = mysql_query($query) or die('Query failed: ' . mysql_error());

      $numresults = mysql_num_rows($result);
      
      // If medication not in list (no med coming up with that name), put it in the list
      // If the medication is in the database, change inList to TRUE once
      if ($numresults == 0){
	//inserting into DB
	$inList = "TRUE";
	$query = "INSERT INTO medications (".$fields.") VALUES (".$InsertQueryPieces[0].", ".$InsertQueryPieces[1].", ".$InsertQueryPieces[2].", ".$InsertQueryPieces[3].", ".$InsertQueryPieces[4].", ".$InsertQueryPieces[5].", ".$InsertQueryPieces[6].", ".$InsertQueryPieces[7].", ".$InsertQueryPieces[8].", ".$InsertQueryPieces[9].", ".$InsertQueryPieces[10].", ".$InsertQueryPieces[11].", ".$InsertQueryPieces[12].", ".$InsertQueryPieces[13].", ".$InsertQueryPieces[14].", ".$InsertQueryPieces[15].", ".$InsertQueryPieces[16].", ".$InsertQueryPieces[17].", ".$InsertQueryPieces[18].", ".$inList.")";	
	
	$InsertResult = mysql_query($query) or die('Query failed: ' . mysql_error());
      } else {
	$row = mysql_fetch_array($result);
	//Should only be one in the database, but there may be more
	// We're only going to set one InList to true so there aren't multiple meds on the list
	$query = "UPDATE medications SET inList = TRUE WHERE ID = ".$row['ID'];
	$UpdateResult = mysql_query($query) or die('Query failed: ' . mysql_error());
      }

      $i++;
   }
   echo "</ol>";
   include 'includes/db-close.php';
 } else {
  ?>
<div id="medication-list">
<?php
  echo "<u>My Medication List</u>\r\n";
  echo "<UL><LI>No Medications Selected</LI></UL>\r\n";  
 }
?>
</div>
<?php
 } // end of else not a GET statement
?>




<?php 
   include 'includes/wizard-large-button.php';
   include 'includes/wizard-simple-footer.php';

?>